<?php


function userForm()
{
    $fields[]   = new Text('login', $_REQUEST['login'], 'text', 'Login' );
    $fields[]   = new Text('username', $_REQUEST['username'], 'text', 'Nome' );
    $fields[]   = new Text('email', $_REQUEST['email'], 'text', 'Email' );
    $fields[]   = new SSelect('class', $_REQUEST['class'], array('admin','user','none'), 'text','Classe' );
    $fields[]   = new Password('passwd', $_REQUEST['passwd'], 'text', 'Senha' );
    $fields[]   = new Button('save', 'Criar Usuário', "ajaxCall('formUser',null, 'ajaxReturn','createUser', 'admin.php')", 'button');
    $fields[]   = new Div('ajaxReturn', '');
    $form       = new Easyform('formUser', $fields,'admin.php', 'post',null,'easyForm');
    $divUser    = new DivForm('divUser', $form, 300, 170, "Usuários",  'divForm') ;
    $divUser    ->setPosition(20,60);
    put($divUser);
}

function createUser()
{
    global $login;
    if ( $login->createUser($_REQUEST) )
    {
        put( new Span(null, 'Operação efetuada com Sucesso!', 'label-alert') ); 

    }
    else
    {
        put( new Span(null, 'Falha na Operação', 'label-alert') ); 
    }
}

/**
 *  Mount the Login Form
 */
/*function userForm()
{
    global $login;
    global $page;
    //Delete
    if($deleteAction = getActionResult('divAdmin','remove_') );
    {
        $login->removeUser($deleteAction);
    }
    $page->script->addJsCode("
                    function validate_form(thisform)
                    {
                        var login    = validatorPost('login'   , '10', true);
                        var passwd   = validatorPost('passwd'  , '10', true);
                        var username = validatorPost('username', '0' , true);
                        return (login && passwd && username);
                    }

                    function validate_grid(thisform)
                    {
                        var answer = confirm('Tem certeza que deseja efetuar a operação?')

                        if (answer)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                ");
    //Form Mount
    $columns        = array('login', 'username', 'email', 'class', 'count', 'lastIp');
    $widths         = array(100,200, 200, 50, 50, 80);
    $iLogin         =  new textField('login',  null, 'Login', 100, 90, 20, 20, 30, 'text');
    $iLogin         -> getField()->addEvent('onkeypress', "validatorKeyPress(this, event, '10')");
    $iLogin         -> getField()->addEvent('onblur', "validatorBlur(this, '10', true)");
    $tableAction    = getActionResult('divAdmin');
    if ($tableAction)
    {
            $result = $login->getUserData($tableAction);

            foreach ($result as $row)
            {

                $loginValue     = $tableAction;
                $usernameValue  = $row['username'];
                $emailValue     = $row['email'];
                $passwordValue  = '';
                $classValue     = $row['class'];
            }
    }
    else
    {
            $loginValue     = $_POST['login'];
            $usernameValue  = $_POST['username'];
            $emailValue     = $_POST['email'];
            $passwordValue  = $_POST['passwd'];
            $classValue     = $_POST['class'];
    }
    $iLogin     ->getField()->setValue( $loginValue );
    if ($tableAction)
    {
        $iLogin->getField()->add('readonly', 'readonly');
    }
    $iUsername  = new textField('username',  null, 'Nome', 100, 300, 20, 230,30, 'text');
    $iUsername  ->getField()->setValue($usernameValue);
    $iClass     = new SelectField('class',  array('admin'), 'Classe', 100, 90, 20, 20,60, 'text');
    $iClass     ->getField()->setValue($classValue);
    $iEmail     =  new textField('email',  null, 'E-mail', 100, 300, 20, 230,60, 'text');
    $iEmail     -> getField()->setValue($emailValue);
    $ipwd       = new textField('passwd',  'password', 'Senha', 100, 90, 20, 20,90,
 'text');
    $ipwd       ->getField()->addEvent('onkeypress', "validatorKeyPress(this, event, '10')");
    $ipwd       ->getField()->addEvent('onblur', "validatorBlur(this, '10')");
    $ipwd       -> getField()->setValue($passwordValue);
    $iSave      = new submit('save',  'Salvar', 'columnTitle');
    $iSave      -> setBounds(330,90, 90,30);
    $iSave      -> onClick('saveUser');
    $list       = $login->listAll();
    $temp       = new Grid('divAdmin', $columns, $list, $widths, 20,130, 24, 'columnTitle', 'text');
    $divAdmin   = $temp -> getTable();
    $width      = $divAdmin->getStyle()->getValue('height');
    $loginForm  = new form('formUser', array(    $iLogin,
                                                $iUsername,
                                                $iClass,
                                                $iEmail,
                                                $ipwd,
                                                $iSave
                                            ), 'admin.php', 'post', 'return validate_form()');
    $divAdminForm = new form('formUser', array($divAdmin), 'admin.php', 'post', 'return validate_grid()', 'easyForm');
    $divUser      = new DivForm('divUser',  array($loginForm,$divAdminForm ), 800,$width+150, "Usuários",  'divForm') ;
    $divUser      ->setPosition(20,60);
//    $page->addContent($divUser);
    put($divUser);
}*/


/**
 * Called when user choose to save the data in form...
 */
/*function saveUser()
{
    global $login;
    global $page;
    if ( $login->createUser($_POST) )
    {
        $page->script->alert("Operação efetuada com Sucesso!");
    }
    else
    {
        $page->script->alert("Falha na Operação!");
    }
}*/

?>
